import { lazy } from 'react';
import { t } from '@/locale';
import config from '@/config';
import moduleRoutes from './routes';

import Redirect from '@/pages/redirect';

const Dashboard = lazy(() => import('@/modules/dashboard'));
const BasicLayout = lazy(() => import('@/layout/BasicLayout'));
const BlankLayout = lazy(() => import('@/layout/BlankLayout'));
const Nomatch = lazy(() => import('@/pages/nomatch'));

export const getLocalRoutes = () => {
  let result: any[] = [];
  if (config.isMainApp) {
    const localRoutes = localStorage.getItem('sub_routes');
    if (localRoutes) {
      try {
        result = JSON.parse(localRoutes).map((x) => ({
          ...x,
          component: BlankLayout,
        }));
      } catch (err) {
        // ...
      }
    }
  }
  return result;
};

// subView 判断
const isSubView = (path = ''): boolean => {
  return path.startsWith('/subview');
};

const routes:any = [
  
  {
    path: '/',
    meta: { title: t('app.global.home') },
    component: BasicLayout,
    routes: [
      {
        path: '/',
        exact: true,
        redirect: '/home',
      },
      {
        path: '/home',
        meta: { title: t('app.global.dashboard'), bgColor: true, keepAlive: false },
        component: Dashboard,
      },
      ...moduleRoutes
        .map((x) => x.routes)
        .flat()
        .map((x) => {
          return x;
        }),
      {
        path: '/redirect/:path(.*)',
        component: Redirect,
      }
    ],
  }
];

export default routes;
